package cc.ycws.web.controller;

import cc.ycws.core.domain.Location;
import cc.ycws.core.domain.Unitdoor;
import cc.ycws.core.service.LocationService;
import cc.ycws.core.service.UnitdoorService;
import com.alibaba.fastjson.JSONObject;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/l")
public class LocationController {

    Logger logger = Logger.getLogger(LocationController.class);
    @Resource
    LocationService locationService;
    @Resource
    UnitdoorService unitdoorService;

    String state_500 = "500";            //表示服务器内部错误
    String state_0 = "0";                //表示请求服务器成功

    /**
     * 自动 获取所有小区地址信息
     *
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/showAll", method = RequestMethod.GET, produces = "text/html;charset=UTF-8")
    public String showAll() {
        try {
            List<Location> list = this.locationService.showAll();
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("code", state_0);
            map.put("list", list);
            JSONObject jsonObject = new JSONObject(map);
            return jsonObject.toString();
        } catch (Exception e) {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("code", state_500);
            JSONObject jsonObject = new JSONObject(map);
            return jsonObject.toString();
        }
    }

    /**
     * 获取指定小区对应的楼栋信息
     *
     * @param record
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/showUnit", method = RequestMethod.GET, produces = "text/html;charset=UTF-8")
    public String showUnit(Unitdoor record) throws Exception {
        try {
            String address = new String(record.getAddress().getBytes("ISO-8859-1"), "utf-8");
            record.setAddress(address);
            record.setNumber("all");
            List<Unitdoor> list = this.unitdoorService.showUnitdoor(record);
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("code", state_0);
            map.put("list", list);
            JSONObject jsonObject = new JSONObject(map);
            return jsonObject.toString();
        } catch (Exception e) {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("code", state_500);
            JSONObject jsonObject = new JSONObject(map);
            return jsonObject.toString();
        }
    }
}
